<?php
/**
 * Created by PhpStorm.
 * User: xutao
 * Date: 2018/11/8
 * Time: 10:21
 */
//登录控制器
namespace app\admin\controller;
use app\admin\model\Admin;
use think\Controller;
use think\Session;

class Login extends Controller{

    //登录
    public function login(){

        if(request()->isPost()){
            //接受数据
            $data = input('param.');
            $where = [
                'username'=>$data['username'],
                'password'=>md5($data['password'])
            ];
            //查询用户是否存在
            $admin = Admin::where($where)->find();
            if(!$admin) return json(['code'=>1,'data'=>$admin,'msg'=>'账号或密码错误']);
            if($admin->status == 0) return json(['code'=>2,'data'=>$admin,'msg'=>'该账号禁止登录']);
            Admin::where('id',$admin->id)->update([
                'loginnum'=>$admin->loginnum +1,
                'last_login_time'=>time(),
                'last_login_ip'=>$_SERVER['REMOTE_ADDR']
            ]);

            Session::set('admin_id',$admin->id);
            return json(['code'=>200,'data'=>$data,'msg'=>$_SERVER]);
        }else{
            //登录页面
            return $this->fetch();
        }
    }
    //退出登录
    public function login_out(){
        Session::clear();
        return $this->redirect('/admin/login/login');

    }





}